package ts.group.webapp.market.controller.web;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ts.group.webapp.market.common.core.util.ResponseData;
import ts.group.webapp.market.common.core.web.controller.BaseController;
import ts.group.webapp.market.model.TShopInfo;
import ts.group.webapp.market.service.IDistService;
import ts.group.webapp.market.service.IShopService;

import com.starit.common.dao.support.Pagination;
import com.starit.common.dao.support.PaginationRequest;

@Controller
@RequestMapping("/shop")
public class ShopController extends BaseController{
	
	@Autowired
	private IShopService  IShopService;
	@Autowired
	private IDistService	IDistributorService; 
	
	
	
	/**
	 * 新增店铺，只接受POST请求
	 */
	@RequestMapping(value = "/insertTShop", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData insertTShop(TShopInfo tShopInfo) throws Exception {

		/*店铺编号必须唯一*/
		if(!IShopService.getShopByShopCode(tShopInfo.getShopCode()).isEmpty()){
			return new ResponseData(true , "(店铺编码重复)");
		}
		tShopInfo.setCreateTime(new Date());
		IShopService.createShop(tShopInfo);
		return ResponseData.SUCCESS_NO_DATA ;
	}
	
	
	/**
	 * 更新店铺, 只接受POST请求
	 * 如果经销商编码不为空的话，shopOwner必须在是经销商表里有记录
	 */
	@RequestMapping(value = "/updateTShop", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData updateTShop(TShopInfo tShopInfo) {
		/*经销商编号必须存在*/
		if(IDistributorService.getDistributorByCode(tShopInfo.getShopOwner()).isEmpty()){
			return new ResponseData(true , "(经销商不存在)");
		}
		TShopInfo old = IShopService.getShopByShopCode(tShopInfo.getShopCode()).get(0);
		old.setShopAddr(tShopInfo.getShopAddr());
		old.setShopCity(tShopInfo.getShopCity());
		old.setShopCountry(tShopInfo.getShopCountry());
		old.setShopName(tShopInfo.getShopName());	   //店铺名称
		old.setShopOwner(tShopInfo.getShopOwner());   //经销商编码
		IShopService.updateShop(old);
		return ResponseData.SUCCESS_NO_DATA;
	}
	
	/**
	 * 删除店铺, 只接受POST请求
	 */
	@RequestMapping(value = "/removeTshop", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteTShopInfo(Long[] ids) {
		boolean left = false;
		for(Long shopId : ids){
			if(IDistributorService.getDistributorByShopId(shopId).isEmpty()){ /*判断店铺是否存在经销商*/
					IShopService.removeShop(shopId);
			}else{
				left = true;
			}
		}
		return left? new ResponseData(true,"有部分店铺有经销商不能被删除"):ResponseData.SUCCESS_NO_DATA;
	}

	
	/**
	 * 店铺管理，查询店铺信息，按照店铺优先级降序排序
	 */
	@RequestMapping("/pageQueryTShops")
	@ResponseBody
	public Pagination<TShopInfo> pageQueryTShops(
			@RequestParam("start") int startIndex,
			@RequestParam("limit") int pageSize, TShopInfo tShopInfo,
			@RequestParam(required = false) String sort,
			@RequestParam(required = false) String direction){
		
		PaginationRequest<TShopInfo> pageRequest = new PaginationRequest<TShopInfo>(startIndex, pageSize);
		
		
		if (StringUtils.hasText(sort) && StringUtils.hasText(direction)){
			pageRequest.addOrder(sort, direction);
		}
		if (StringUtils.hasText(tShopInfo.getShopCode())) {
			pageRequest.addLikeCondition("shopCode", tShopInfo.getShopCode());
		}
		if (StringUtils.hasText(tShopInfo.getShopCountry())) {
			pageRequest.addLikeCondition("shopCountry", tShopInfo.getShopCountry());
		}
		if (StringUtils.hasText(tShopInfo.getShopCity())) {
			pageRequest.addLikeCondition("shopCity", tShopInfo.getShopCity());
		}
		if (StringUtils.hasText(tShopInfo.getShopName())) {
			pageRequest.addLikeCondition("shopName", tShopInfo.getShopName());
		} 
		Pagination<TShopInfo> page = IShopService.findAllForPage(pageRequest);
		return page ;
		
	}
}
